package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseConfirmDialogFragment;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.DataStatus;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.io.Serializable;
import org.json.JSONException;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/6/29 14:28
 */
public abstract class BaseOutDetailFragment<T extends Serializable> extends BaseFragment {
  public static final String EXTRA_VALUE =
      BaseOutDetailFragment.class.getSimpleName() + ".EXTRA_VALUE";
  private final int ADD = 2;
  private final int NEW = 3;
  private final int SUB = 4;
  private final int PUBLISH = 5;
  private static final int REQUEST_DELETE = 6;
  private static final int REQUEST_SUBMIT = 7;

  private int menuType;
  protected T mContent;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom("零售出库详情");
    mContent = (T) getArguments().get(EXTRA_VALUE);

    if (mContent != null) {
      String mDataStatus = getDataStatus();
      if (DataStatus.NEW.equalsIgnoreCase(mDataStatus) || DataStatus.ABORT.equalsIgnoreCase(
          mDataStatus)) {
        menuType = NEW;
      } else if (DataStatus.WAIT_CHECK.equalsIgnoreCase(mDataStatus)) {
        menuType = SUB;
      } else {
        menuType = PUBLISH;
      }
    }
    if (mContent == null) {
      menuType = ADD;
    }
  }

  protected String getDataStatus() {
    return "";
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    switch (menuType) {
      //草稿状态：保存、保存并提交
      case ADD:
        inflater.inflate(R.menu.menu_save_submit, menu);
        menu.getItem(1).setTitle(R.string.submit);
        break;
      //草稿状态：保存、提交、删除
      case NEW:
        inflater.inflate(R.menu.menu_save_submit_delete, menu);
        menu.getItem(1).setTitle(R.string.submit);
        break;
      //提交状态：返回修改
      case SUB:
        inflater.inflate(R.menu.menu_stop, menu);
        break;
      //审核通过,已出库
      case PUBLISH:
        break;
      default:
        break;
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();

        return true;
      case R.id.menu_item_save:
        //保存
        if (isCorrectInput()) {
          String SAVE = "save";
          volleyPostSave(SAVE);
        }
        return true;
      case R.id.menu_item_submit:
        //保存并提交
        boolean isCorrectSubmit = isCorrectInput();
        if (isCorrectSubmit) {
          showDialog(BaseConfirmDialogFragment.newInstance(R.string.start_up_hint), REQUEST_SUBMIT);
        }
        return true;
      case R.id.menu_item_delete:
        //删除
        showDialog(BaseConfirmDialogFragment.newInstance(R.string.deleteConfirm), REQUEST_DELETE);
        return true;
      case R.id.menu_item_stop:
        // 返回修改
        volleyPostAbort();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  private boolean isCorrectInput() {
    return false;
  }

  private void volleyPostSave(String type) {
    String url = ApiConst.getAbsoluteUrl(initSaveUrl());
    ParamsNotEmpty params = initSaveParam();
    MyUtils.showDialogLoading(getActivity());
    mActivity.volleyPost(url, params.getParams(), getResponse());
  }

  private void volleyPostAbort() {
    String url = ApiConst.getAbsoluteUrl(initAbortUrl());
    ParamsNotEmpty params = initAbortParam();
    MyUtils.showDialogLoading(getActivity());
    mActivity.volleyPost(url, params.getParams(), getResponse());
  }

  protected ParamsNotEmpty initAbortParam() {
    return new ParamsNotEmpty();
  }

  protected abstract String initAbortUrl();

  protected ParamsNotEmpty initSaveParam() {
    return new ParamsNotEmpty();
  }

  protected abstract String initSaveUrl();

  protected ParamsNotEmpty initDelParam() {
    return new ParamsNotEmpty();
  }

  protected abstract String initDelUrl();

  //删除
  private void volleyPostDelete() {
    String url = ApiConst.getAbsoluteUrl(initDelUrl());
    ParamsNotEmpty params = initDelParam();
    MyUtils.showDialogLoading(getActivity());
    mActivity.volleyPost(url, params.getParams(), getResponse());
  }

  public VolleyResponse getResponse() {
    return new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {

      }

      @Override public void onVolleyResponse(String arg0) throws JSONException {
        MyUtils.dismissDialog();
        SuccessMessage message = new Gson().fromJson(arg0, SuccessMessage.class);
        if (message.getResult() == null) {
          ToastUtil.show(R.string.failure_operation);
          return;
        }
        if (message.getResult() != null && SuccessMessage.SUCCESS.equalsIgnoreCase(
            message.getResult())) {
          ToastUtil.show(R.string.success_operation);
          getActivity().setResult(Activity.RESULT_OK);
          getActivity().finish();
        }
        if (message.getResult() != null && SuccessMessage.FAILURE.equalsIgnoreCase(
            message.getResult())) {
          ToastUtil.show(message.getMessage());
        }
      }
    };
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_DELETE) {
      volleyPostDelete();
    }
    if (requestCode == REQUEST_SUBMIT) {
      String RUN = "run";
      volleyPostSave(RUN);
    }

    super.onActivityResult(requestCode, resultCode, data);
  }

  public Fragment newInstance(T content) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_PARAMS, content);
    Fragment fragment = new Fragment();
    fragment.setArguments(args);
    return fragment;
  }
}
